package com.ruoyi.procured.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.api.ordersProduct.OrdersProductApi;
import com.ruoyi.common.api.ordersProduct.dto.OrdersProductDTO;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.OrdersProduct;
import com.ruoyi.procured.domain.ProcuredOrdersProduct;
import com.ruoyi.procured.service.ProcuredOrdersProductService;
import com.ruoyi.procured.mapper.ProcuredOrdersProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Admin
 * @description 针对表【tb_orders_product】的数据库操作Service实现
 * @createDate 2024-10-01 16:35:55
 */
@Service
public class ProcuredOrdersProductServiceImpl extends ServiceImpl<ProcuredOrdersProductMapper, ProcuredOrdersProduct>
		implements ProcuredOrdersProductService, OrdersProductApi {


	@Autowired
	ProcuredOrdersProductMapper ordersProductMapper;

	@Override
	public List<OrdersProduct> selectList(List<Long> ordersIds, Long productId) {
		return ordersProductMapper.selectOrdersProduct(ordersIds, productId);
	}

	/**
	 * 更新采购商品明细待退货数量
	 *
	 * @param id
	 * @param assignQuantity
	 * @return
	 */
	@Override
	public int updateReturnPendingQuantity(Long id, long assignQuantity) {
		return ordersProductMapper.updateReturnPendingQuantity(id, assignQuantity);
	}

	@Override
	public List<String> selectByPlanId(List<Long> collect, Long productId) {
		return ordersProductMapper.selectByPlanId(collect, productId);
	}

	@Override
	public List<OrdersProductDTO> selectByOrdersId(Integer procuredOrderId) {
		List<ProcuredOrdersProduct> procuredOrdersProducts = ordersProductMapper.selectList(new LambdaQueryWrapper<ProcuredOrdersProduct>()
				.eq(ProcuredOrdersProduct::getOrdersId, procuredOrderId)
				.eq(ProcuredOrdersProduct::getDeleteStatus, Constants.NOT_DELETED_STATUS));
		return procuredOrdersProducts.stream().map(
				item -> {
					OrdersProductDTO dto = new OrdersProductDTO();
					BeanUtils.copyProperties(item, dto);
					return dto;
				}
		).collect(Collectors.toList());
	}

	@Override
	public int updateReturnCompletedStatus(Long id) {
		return ordersProductMapper.updateReturnCompletedStatus(id);
	}

	@Override
	public int updateLog(String operationLog, String productId, String procuredOderId) {
		return ordersProductMapper.updateLog(operationLog,productId,procuredOderId);
	}

	@Override
	public int update(Long id) {
		return ordersProductMapper.update(null,new LambdaUpdateWrapper<ProcuredOrdersProduct>()
				.set(ProcuredOrdersProduct::getStyleClass, null)
				.eq(ProcuredOrdersProduct::getId, id)
				.eq(ProcuredOrdersProduct::getDeleteStatus, 0));
	}


}




